<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class ExceptionMiddleware
{

    public function handle($request, Closure $next)
    {
        $result_arr = [];
        try{
            return $next($request);
        }catch (\Exception $e){
            Log::info($e);
//            Log::error($e);
            if($e instanceof \CommonException){
                $result_arr = array(
                    'code'=>$e->getCode(),
                    'message'=>$e->getMessage(),
                );
            }else{
                $result_arr = array(500, $e->getMessage());
            }
        }

        if ($request->ajax()) {
            return response()->json($result_arr);
        } else {
            return response()->json($result_arr);
//                return redirect()->guest('auth/login');
        }
    }
}
